<template>
    <div>
      <BasicTable @register="registerTable">
        <template #toolbar>
            <a-button
                type="primary"
                :loading="expLoading"
                @click="handleAdd" 
            >
            新增
          </a-button>
          <a-button
            type="primary"
            :loading="expLoading"
            @click="handleDownload" 
          >
            二维码
          </a-button>
        </template>
        <template #action="{ record }">
          <TableAction
            :actions="[
              {
                color: 'error',
                label: '已激活',
                ifShow: () => record.type == 1,
              },
              {
                label: '激活',
                ifShow: () => record.type == 0,
                popConfirm: {
                  title: '是否确认激活？',
                  confirm: handleActivate.bind(null, record.id),
                },
              },
              {
                label: '编辑',
                disabled: record.type == 1,
                ifShow: () => record.type == 0,
                onClick: handleEdit.bind(null, record),
              },
              { 
                color: 'error',
                label: '删除',
                disabled: record.type == 1,
                ifShow: () => record.type == 0,
                popConfirm: {
                  title: '是否确认删除？',
                  confirm: handleDelete.bind(null, record.id),
                },
              },
            ]"
          />
        </template>
      </BasicTable>
      <YuEditModal @register="registerModal" @success="handleSuccess" />
    </div>
  </template>
  <script lang="ts">
  import { defineComponent } from 'vue';
import { defHttp } from '@/utils/http/axios';
  export default defineComponent({
    name: 'YuPileList',
  });
  </script>
  <script lang="ts" setup>
  import { ref, watch } from 'vue';
  import { BasicTable, useTable, TableAction } from '@/components/Table';
  import { useModal } from '@/components/Modal';
  import { downloadBlob } from '@/utils/file/download';
  import { useMessage } from '@/hooks/web/useMessage';
  import YuEditModal from './components/yuEditModal.vue';
  const props = defineProps({
    stationId: {
      type: String,
      default: () => [],
    },
  });
  const expLoading = ref(false);
  const { createMessage } = useMessage();
  const [registerModal, { openModal }] = useModal();
  const [registerTable, { reload }] =
    useTable({
      api: '/admin/pile/yu_list',
      columns: [
    //   ID	充电终端名称	集控器地址	CAN	充电终端编号	终端类型	运营商	协议        {
        {
          title: 'ID',
          dataIndex: 'id',
          width: 100,
          fixed: 'left',
        },
        {
          title: '充电终端名称',
          dataIndex: 'name',
          width: 150,
        },
        {
          title: '集控器地址',
          dataIndex: 'controllerSn',
          width: 150,
        },
        {
          title: 'Can',
          dataIndex: 'canIndex',
          width: 100,
          
        },
        {
          title: '充电终端编号',
          dataIndex:'pileSn',
          width: 150,
        },
        {
          title: '终端类型',
          dataIndex:'terminalName',
          width: 150,
        },
        {
          title: '运营商',
          dataIndex: 'operatorMemo',
          width: 150,
        },
        {
          title: '协议',
          dataIndex: 'protocolType',
          width: 100,
          customRender: ({ text }) => {
            if (text == 0) {
                return '万马'
            } else if (text == 1) {
                return '特来电'
            } else if (text == 10) {
                return '云快充'
            }
          }
        },
      ],
      useSearchForm: true,
      showIndexColumn: true,
      indexColumnProps: {
        fixed: 'left',
      },
      pagination: true,
      bordered: true,
      actionColumn: {
        width: 150,
        title: '操作',
        dataIndex: 'action',
        slots: { customRender: 'action' },
        fixed: 'right',
      },
      rowKey: 'id',
      rowSelection: {
        type: 'checkbox',
      },
      beforeFetch: (params) => {
        if (props.stationId) {
            params.id = props.stationId;
        }
        return params;
      }
    });
  watch(() => props.stationId, () => {
    reload();
  })
  async function handleActivate(id: string) {
    const res = await defHttp.post({ url: `/admin/pile/yu_activate/${id}` });
    createMessage.success(res);
    reload();
  }
  async function handleDownload() {
    try {
        expLoading.value = true;
        const res = await defHttp.get({ url: `/admin/file/batchDownload?id=${props.stationId}`, responseType: 'blob', timeout: -1 }, { isReturnNativeResponse: true });
        downloadBlob(res);
    } finally {
        expLoading.value = false;
    }
  }
  function handleAdd() {
    openModal(true, {
        record: { 
          stationId: props.stationId,
        },
        isUpdate: false,
    });
  }
  function handleEdit(record: any) {
    openModal(true, {
        record,
        isUpdate: true,
    });
  }
  async function handleDelete(id: string) {
    await defHttp.post({ url: `/admin/pile/yu_delete/${id}` });
    reload();
  }
  function handleSuccess() {
    reload();
  }
  </script>
  